﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Globalization;
using System.Web.UI.HtmlControls;
using System.Threading;
public class SitePage : BasePage
{
    #region Fields
    protected Guid userloginId = Guid.NewGuid();
    protected string userloginName = string.Empty;
    #endregion

    public SitePage()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region Init,Loads

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        this.Page.Theme = GetPageThem();
    }

    protected override void InitializeCulture()
    {
        base.InitializeCulture();

        string strLang = string.Empty;
        try
        {
            strLang = Session["Language"].ToString();
        }
        catch
        {
            strLang = "vi-VN";
        }

        SetCulturalSystem(strLang);
    }

    public string GetLang(string Key)
    {
        string strValue = string.Empty;
        try
        {
            strValue = (string)GetGlobalResourceObject("Lang", Key);
        }
        catch
        {
            
        }

        return strValue;
    }

    public static string ShowCurrentLang(int Lang)
    {
        if (Lang == 0)
        {
            return "Vietnam";
        }
        else
        {
            return "English";
        }
    }
    private void SetCulturalSystem(string cultural)
    {
        CultureInfo objCul = new CultureInfo(cultural, true);
        DateTimeFormatInfo objDateTimeFormatInfo = objCul.DateTimeFormat;
        objDateTimeFormatInfo.ShortDatePattern = "MM/dd/yyyy";
        objDateTimeFormatInfo.LongDatePattern = "MM/dd/yyyy";
        // objDateTimeFormatInfo.ShortDatePattern = "dd/MM/yyyy";
        // objDateTimeFormatInfo.LongDatePattern = "dd/MM/yyyy";
        objDateTimeFormatInfo.DateSeparator = "/";
        objCul.DateTimeFormat = objDateTimeFormatInfo;

        NumberFormatInfo numFormat = objCul.NumberFormat;
        numFormat.NumberGroupSeparator = ",";
        numFormat.NumberDecimalSeparator = ".";
        numFormat.NumberDecimalDigits = 2;

        numFormat.CurrencyGroupSeparator = ",";
        numFormat.CurrencyDecimalDigits = 2;
        numFormat.CurrencyDecimalSeparator = ".";
        objCul.NumberFormat = numFormat;
        Thread.CurrentThread.CurrentCulture = objCul;
        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultural);

    }
    protected override void OnLoad(EventArgs e)
    {
        try
        {
            MembershipUser user = Membership.GetUser();
            if (user != null)
            {
                userloginId = new Guid(user.ProviderUserKey.ToString());
                userloginName = user.UserName;
            }
            else
            {
                userloginId = Guid.Empty;
                userloginName = string.Empty;
            }
        }
        catch
        {
        }
        base.OnLoad(e);
    }
    #endregion

    #region  Methods
    private string GetPageThem()
    {
        if (this.Request.Url.AbsolutePath.ToLower().Contains("/admin/"))
            return "Admin";
        else return "Default";
    }
    #endregion

    #region  Property
    /// <summary>
    /// Site root URL, EX:  localhost:8001
    /// </summary>
    public string ApplicationRoot
    {
        get
        {
            return Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath + Request.Url.Query, string.Empty) + Request.ApplicationPath;
        }
    }

    protected string ApplicationName
    {
        get
        {
            return Membership.ApplicationName;
        }
    }

    public Guid UserloginId
    {
        get
        {
            return this.userloginId;
        }
    }
    public string GetCurrentPageUrl
    {
        get
        {
            string sPath = System.Web.HttpContext.Current.Request.RawUrl;
            sPath = sPath.Substring(1, sPath.Length - 1);
            int index = sPath.IndexOf('/');
            if (index > 0)
                sPath = sPath.Substring(index + 1, sPath.Length - (index + 1));
            return sPath;
        }
    }
    public string GetPageQuery
    {
        get
        {
            string pageQuery=Request.Url.Query;
            if(pageQuery!=string.Empty)
            {
                if(pageQuery.Substring(0,1)=="?")
                    pageQuery=pageQuery.Substring(1,pageQuery.Length-1);
            }
            return pageQuery;
        }
    }
    public void SetMetaData(string pageTile, string metaDescription, string metaKeywords)
    {
        this.Page.Header.Title = pageTile;
        HtmlMeta metaDes = new HtmlMeta();
        metaDes.Name = "Description";
        metaDes.Content = metaDescription;
        Page.Header.Controls.Add(metaDes);
        HtmlMeta metaKey = new HtmlMeta();
        metaKey.Name = "Keyword";
        metaKey.Content = metaKeywords;
        Page.Header.Controls.Add(metaKey);
        metaKey.Dispose();
        metaDes.Dispose();
    }
    public string GetPageParams()
    {
        FilterFields Params = new FilterFields();
        return Params.ToString(FilterFieldType.All, "", ViewState);
    }
    public string GetPageParams(FilterFieldType preserve)
    {
        FilterFields Params = new FilterFields();
        return Params.ToString(preserve, "", ViewState);
    }
    public string GetPageParams(FilterFieldType preserve, string removeList)
    {
        FilterFields Params = new FilterFields();
        return Params.ToString(preserve, removeList, ViewState);
    }

    public static string ShowPrice(float price)
    {
        string strPrice = string.Empty;
        if (price > 0)
            strPrice = string.Format("{0:N0} VND", price);

        return strPrice;
    }

    public static string ShowPrice(float wasPrice, float nowPrice)
    {
        string strPrice = string.Empty;
        if (wasPrice > 0)
        {
            strPrice = string.Format(@"<span class=""wasprice""><s>{0:N0} VNĐ</s></span><br><span class=""nowprice"">{1:N0} VNĐ</span>", wasPrice, nowPrice);
        }
        else if (nowPrice > 0)
            strPrice = string.Format("{0:N0} VNĐ", nowPrice);

        return strPrice;
    }

    public static string ShowDetailPrice(float wasPrice, float nowPrice)
    {
        string strPrice = string.Empty;
        if (wasPrice > 0)
        {
            strPrice = string.Format(@"<span class=""wasprice"">Giá cũ <s>{0:N0} VNĐ</s></span><br><span class=""nowprice"">Giá mới {1:N0} VNĐ</span>", wasPrice, nowPrice);
        }
        else if (nowPrice > 0)
            strPrice = string.Format("Giá {0:N0} VNĐ", nowPrice);
        else
        {
            strPrice = string.Format("Giá call");
        }
        return strPrice;
    }
    #endregion

    public static string Language(string lang)
    {
        string str = string.Empty;
        if (HttpContext.Current.Session["Language"] != null)
        {
            if (HttpContext.Current.Session["Language"] != null)
            {
                //Resources.VN.
            }
        }


        return str;
    }

    public int GetCurrentLang()
    {
        int i = 0;
        try
        {
            if (HttpContext.Current.Session["Language"] != null)
            {
                if (HttpContext.Current.Session["Language"] != "vi-VN")
                {
                    i = 1;
                }
            }
        }
        catch
        {

        }

        return i;
    }
}